package com.aaron.kjviewutils.driving;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aaron.kjviewutils.JSONUtil;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/3.
 */

public class DrivingTopicFragment extends Fragment {

    @Bind(R.id.weCatagoryRecyclerView)
    RecyclerView weCatagoryRecyclerView;

    private DrivingTopicActivity rootAty;
    private View rootView;
    private List<DrivingTopicBean> datas;
    private DrivingTopicAdapter adapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (DrivingTopicActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_wechat, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initContent();
    }

    private void initContent()
    {
        datas = new ArrayList<DrivingTopicBean>();
        Bundle bundle = getArguments();
        if (bundle != null) {
            String source = bundle.getString("data");
            JsonParser jsonParser = new JsonParser();
            JsonArray jsonArray = jsonParser.parse(source).getAsJsonArray();
            int size = jsonArray.size();
            if(size>0)
            {
                datas.clear();
                Iterator<JsonElement> iterator = jsonArray.iterator();
                List<DrivingTopicBean> stockInners = new ArrayList<DrivingTopicBean>();
                while (iterator.hasNext())
                {
                    JsonElement jsonElement = iterator.next();
                    if(jsonElement.isJsonObject())
                    {
                        JSONUtil<DrivingTopicBean> jsonUtil = new JSONUtil<DrivingTopicBean>();
                        DrivingTopicBean topic = new DrivingTopicBean();
                        topic = jsonUtil.toBean(jsonElement.toString(), topic);
                        stockInners.add(topic);
                    }
                }
                datas.addAll(stockInners);
                // 错列网格布局
                weCatagoryRecyclerView.setLayoutManager(new LinearLayoutManager(rootAty));
                adapter = new DrivingTopicAdapter(rootAty, rootAty, datas);
                weCatagoryRecyclerView.setAdapter(adapter);
                weCatagoryRecyclerView.setItemAnimator(new DefaultItemAnimator()); // 默认动画
                weCatagoryRecyclerView.addItemDecoration(new RecycleViewDividerForList(rootAty, LinearLayoutManager.HORIZONTAL));
                adapter.setmOnItemClickListener(new DrivingTopicAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        DrivingTopicBean drivingTopic = datas.get(position);
                        Bundle bundle = new Bundle();
                        bundle.putString("cid", drivingTopic.cid);
                        bundle.putString("type", "no.3");
                        rootAty.showActivity(rootAty, DrivingDocActivity.class, bundle);
                    }
                });
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
